package com.lee.query;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/3/9
 */
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class QueryNB {
    private static RestHighLevelClient client;
    private static ObjectMapper objectMapper;
    @BeforeAll
    public static void init() {
        client = new RestHighLevelClient(RestClient.builder(HttpHost.create("https://elasticsearch-dev.souban.io")));
        objectMapper = new ObjectMapper();
    }
    @AfterAll
    private static void destory() throws IOException {
        client.close();
    }

    @Test
    public void queryAll() throws IOException {

        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(queryBuilder);
        SearchRequest request = new SearchRequest();
        request.indices("user");
        request.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        searchResponse.getHits().forEach(i -> System.out.println(i.getSourceAsString()));
    }

    @Test
    public void queryWithCondition() throws IOException {

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("gender", "男");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(termQueryBuilder);
        SearchRequest request = new SearchRequest();
        request.indices("user");

        request.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        searchResponse.getHits().forEach(i -> System.out.println(i.getSourceAsString()));
    }

    @Test
    public void queryWithPage() throws IOException {

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("gender", "男");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(termQueryBuilder);
        SearchRequest request = new SearchRequest();
        request.indices("user");
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(2);

        request.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        searchResponse.getHits().forEach(i -> System.out.println(i.getSourceAsString()));
    }

    @Test
    public void queryWithSort() throws IOException {

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("gender", "男");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(termQueryBuilder);
        SearchRequest request = new SearchRequest();
        request.indices("user");

//        SortBuilder.sor
//        searchSourceBuilder.sort("age");

        searchSourceBuilder.sort("age", SortOrder.DESC);


        request.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        searchResponse.getHits().forEach(i -> System.out.println(i.getSourceAsString()));
    }


    @Test
    public void queryWithSourceFilter() throws IOException {

        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("gender", "男");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(termQueryBuilder);
        SearchRequest request = new SearchRequest();
        request.indices("user");

//        SortBuilder.sor
//        searchSourceBuilder.sort("age");

//        searchSourceBuilder.sort("age", SortOrder.DESC);

        searchSourceBuilder.fetchSource("name", "");

        request.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        searchResponse.getHits().forEach(i -> System.out.println(i.getSourceAsString()));
    }




}
